<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Page category->article
 * Overview of articles
 * use article.tpl.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $subpage, $refValidate)
{
	$refPage->assign('messagebox', '');
	$refPage->assign('articleListHelp', help('articleList', $page));
	$refPage->assign('articlePageHelp', help('articlePage', $page, TRUE));
	
	if (!isset($_SESSION['articleFilter']))
		$_SESSION['articleFilter'] = '0';
	$filterId = '';

	if (isset($_GET['func']))
	{
		$idx = 0;
		if (isset($_GET['id']))
		{
			$idx = $refValidate->testNumber($_GET['id'],
				_('Error in submittet Article-ID-value - Set to 0.'));
		}

		if ($_GET['func'] === 'pagi')
		{
			$number = intval($_GET['nr']);

			$filterId = _setFilter($refValidate);
			$_SESSION['pageA'] = $number;

			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($_GET['func'] === 'edit')
		{
			// get the editor-page
			$pagefile = 'module/page/article/article.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Cancel'))
		{
			$filterId = _setFilter($refValidate);
			// get the artcle-table
			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Save'))
		{
			// save data
			$pagefile = 'module/page/article/article.page.save.php';
			if (is_file($pagefile))
				include_once $pagefile;
			$filterId = _setFilter($refValidate);
			// get the category-table
			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Refresh Table'))
		{
			$filterId = _setFilter($refValidate);
			// get the artcle-table
			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Refresh'))
		{
			// save data
			$pagefile = 'module/page/article/article.page.save.php';
			if (is_file($pagefile))
				include_once $pagefile;
			$filterId = _setFilter($refValidate);

			// get the editor-page
			$pagefile = 'module/page/article/article.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Delete'))
		{
			// open the "Delete"-Window
			if (!empty($_POST['marker']))
			{
				$pagefile = 'module/page/article/article.page.delete.php';
			}
			else
			{
				$pagefile = 'module/page/article/article.page.table.php';
			}
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Delete now'))
		{ // copy article to article-basket and delete the source
			// and set new order-numbers!
			$deleteIds = explode('-', $refValidate->cleanslash($_POST['deleteIds']));

			foreach ($deleteIds as $value)
			{
				db::getInstance()->where('product_id', $value);
				$art = db::getInstance()->getOne('product');

				if ($art !== NULL)
				{
					db::getInstance()->rawQuery("INSERT INTO product_basket  SELECT * " .
						"FROM product WHERE product.product_id = '" . $value . "'");
					db::getInstance()->where("product_id", $value);
					$artCopy = db::getInstance()->getOne("product_basket");

					if ($artCopy !== NULL)
					{
						db::getInstance()->rawQuery("DELETE FROM product WHERE product_id = '" .
							$value . "'");
						message::setNoteRow($art['product_id'] . ' ' . $art['product_name'] .
							' -> ' . _('Data were moved into the basket.'));
					}
					else
					{
						message::setErrorRow($art['product_id'] . ' ' . $art['product_name'] .
							' -> ' . _('Faults delete the data - move/delete.'));
					}

					$ordernr = 1;
					db::getInstance()->where("product_category", $art['product_category']);
					db::getInstance()->orderBy('product_order', 'ASC');
					$article = db::getInstance()->get("product");

					if ($article !== NULL)
					{
						foreach ($article as $valuex)
						{
							$data = Array('product_order' => $ordernr);
							db::getInstance()->where('product_id', $valuex['product_id']);
							db::getInstance()->update('product', $data);
							$ordernr++;
						}
					}

					if (!empty($art['product_small_image']))
					{ // Move small image in basket-path
						if (!is_dir('../images/basket/' . ARTICLE_IMAGE_PATH))
						{
							mkdir('../images/basket/' . ARTICLE_IMAGE_PATH);
						}
						if (!is_dir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category']))
						{
							mkdir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category']);
						}
						if (!is_dir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category'] . '/' . ARTICLE_IMAGE_THUMB_PATH))
						{
							mkdir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category'] . '/' . ARTICLE_IMAGE_THUMB_PATH);
						}

						if (rename('../images/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category'] . '/' . ARTICLE_IMAGE_THUMB_PATH . '/' .
								$art['product_small_image'],
								'../images/basket/' .
								ARTICLE_IMAGE_PATH . '/' . $art['product_category'] . '/' .
								ARTICLE_IMAGE_THUMB_PATH . '/' . $art['product_small_image']))
						{
							// TODO: Delete Dirs if empty ???
							/* rmdir('../images/' . $refConfig->articleImagePath . '/' . $art->product_category . '/' .
							  $refConfig->categoryImageThumbnailPath);
							  if (empty($cat->category_image))
							  rmdir('../images/' . $refConfig->articleImagePath . '/' . $art->product_category);
							 */
							message::setNoteRow($art['product_id'] . ' ' . $art['product_name'] .
								' -> ' . _('The old small image file was moved successfully. '));
						}
					}

					if (!empty($art['product_image']))
					{ // Move image in basket-path
						if (!is_dir('../images/basket/' . ARTICLE_IMAGE_PATH))
						{
							mkdir('../images/basket/' . ARTICLE_IMAGE_PATH);
						}
						if (!is_dir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category']))
						{
							mkdir('../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category']);
						}

						if (rename('../images/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category'] . '/' . $art['product_image'],
								'../images/basket/' . ARTICLE_IMAGE_PATH . '/' .
								$art['product_category'] . '/' . $art['product_image']))
						{
							// TODO: Delete Dirs if empty ???
							/* rmdir('../images/' . $refConfig->categoryImagePath . '/' . $art->product_category); */
							message::setNoteRow($art['product_id'] . ' ' . $art['product_name'] .
								' -> ' . _('The old image file was moved successfully. '));
						}
					}
				}
			}
			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('New'))
		{
			// create a new article
			$data = Array('product_name' => 'New Article',
			 'product_price_id' => 0);
			db::getInstance()->insert('product', $data);

			db::getInstance()->where('product_name', 'New Article');
			$result = db::getInstance()->getOne('product');
			$idx = $result['product_id'];
			
			$data = array('product_create' => date("Y-m-d H:i:s"));
			db::getInstance()->where('product_id', $idx);
			db::getInstance()->update('product', $data);

			// call the editor page for the new article
			$pagefile = 'module/page/article/article.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Save order'))
		{  // Save the manual given ordernumbers
			$pagefile = 'module/page/article/article.page.morder.php';
			if (is_file($pagefile))
				include_once $pagefile;

			$filterId = _setFilter($refValidate);
			$pagefile = 'module/page/article/article.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Copy'))
		{ // Copy the marked article into the same category
			if (!empty($_POST['marker']))
			{
				// copy one article
				$pagefile = 'module/page/article/article.page.copy.php';
				if (is_file($pagefile))
					include_once $pagefile;
			}
			// get the editor-page
			$pagefile = 'module/page/article/article.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
	}
	else if (isset($_GET['filterId']))
	{
		$_SESSION['articleFilter'] = $refValidate->cleanslash($_GET['filterId']);
		$filterId = _setFilter($refValidate);
		// get the artcle-table
		$pagefile = 'module/page/article/article.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	else if (isset($_GET['active']))
	{
		// activate or deactivate article
		$pagefile = 'module/page/article/article.page.action.php';
		if (is_file($pagefile))
			include_once $pagefile;

		$filterId = _setFilter($refValidate);
		$pagefile = 'module/page/article/article.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	else if (isset($_GET['order']))
	{  // push up position of selected data record, if not No.1
		$pagefile = 'module/page/article/article.page.order.php';
		if (is_file($pagefile))
			include_once $pagefile;

		$filterId = _setFilter($refValidate);
		$pagefile = 'module/page/article/article.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	else
	{
		// get the artcle-table
		$filterId = _setFilter($refValidate);
		$pagefile = 'module/page/article/article.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
}

/*
 * Set articlefilter
 * 
 * @param object $refValidate	reference to validate-class
 * @return mysql-filter
 */

function _setFilter($refValidate)
{
	// Set a filter(-category) to article-view
	if (isset($_POST['categoryListx']))
		$id = intval($_POST['categoryListx']);
	else
		$id = intval($_SESSION['articleFilter']);

	if ($_SESSION['articleFilter'] <> $id)
	{
		$_SESSION['articleFilter'] = $id;
		$_SESSION['pageA'] = 1;
	}

	if ($_SESSION['articleFilter'] == '0')
		return "";
	else
		return " WHERE product_category = '" . $_SESSION['articleFilter'] . "'";
}
